package JDBC.model;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Fou Jack of all trades and master of none.
 * @date 2022/1/6 11:53
 */
public class UserDAOSupportImpl extends JdbcDaoSupport implements IUserDAO {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringDao.xml"});
        BeanFactory factory = context;
        IUserDAO userDAO = (IUserDAO) factory.getBean("userDao");
//        User user = new User("lalal",26,333.33);
//        userDAO.AddUser(user);
        List<User> list = userDAO.QueryALLUser();
        for(User u:list){
            System.out.println(u.toString());
        }
    }


    //使用JdbcTemplate时重写的方法
//    @Override
//    public List<User> QueryALLUser() {
//        String sql = "select id,name,age,money from t_user order by id desc";
//        List<Map<String,Object>> list = getJdbcTemplate().queryForList(sql);
//        List<User> userList = new ArrayList<User>();
//        for (Map<String,Object> row:list){
//            User user = new User();
//            user.setId((Integer)row.get("id"));
//            user.setName((String) row.get("name"));
//            user.setAge((Integer)row.get("age"));
//            user.setMoney(Double.parseDouble((row.get("money").toString())));
//            userList.add(user);
//        }
//        return userList;
//    }

    //使用MappingSqlQuery重写的查询方法
    @Override
    public List<User> QueryALLUser() {
        String sql = "select id,name,age,money from t_user where ?";
        UserMappingSqlQuery userMappingSqlQuery = new UserMappingSqlQuery();
        userMappingSqlQuery.setDataSource(getDataSource());
        userMappingSqlQuery.setSql(sql);
        userMappingSqlQuery.setParameters(new SqlParameter(Types.VARCHAR));
        userMappingSqlQuery.compile();
        return userMappingSqlQuery.execute(new Object[]{new String("1=1")});

    }

    @Override
    public Boolean AddUser(User user) {
        String sql = "insert into t_user (name,age,money) values (?,?,?)";
        int row = getJdbcTemplate().update(sql,new Object[]{user.getName(),user.getAge(),user.getMoney()});
        if (row>0){
            System.out.println("数据新增成功！");
            return true;
        }
        return false;
    }

    @Override
    public Boolean transfer(int formUserId, int toUserId, float transferMoney) {
        Boolean out = outInMoney(formUserId , transferMoney);
        //int i = 1/0;   //事务回滚时使用
        Boolean in = outInMoney(toUserId,transferMoney);
        return out&in;
    }


    private Boolean outInMoney(int toUserId,float money){
        String sql = "update t_user set money =money+? where id=?";
        int row = getJdbcTemplate().update(sql,new Object[]{money,toUserId});
        if (row>0){
            return true;
        }
        return false;
    }
}
